<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>防抖/节流</title>
  <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
</head>
<body>
  <input type="text" id="inpText">
  <button id="btn">点我</button>
  <section class="wrap">
    <div class="content">1</div>
  </section>
  <style>
    .wrap {
      width: 200px;
      height: 400px;
      overflow: scroll;
    }
    .content {
      height: 200%;
      background: aqua;
    }
  </style>
  <script>
    const wrap = document.querySelector('.wrap')
    wrap.addEventListener('scroll', throttle())
    function throttle(e) {
      let canUse = true
      return function () {
        if (!canUse) return
        canUse = false
        if (this.scrollTop - this.clientHeight <= 100) {
          console.log('scroll to end')
        }
        setTimeout(() => {
          canUse = true
        }, 2000)
      }
    }
    // wrap.addEventListener('scroll', _.throttle(function(){
    //   if (this.scrollTop - this.clientHeight <= 100) {
    //       console.log('scroll to end')
    //   }
    // }, 2000, {
    //   trailing: false
    // }))
    inpText.addEventListener('keyup', check())
    function check() {
      let timer
      return function() {
          clearTimeout(timer)
          timer = setTimeout(() => {
            console.log('check something...')
          }, 800)
      }
    }
  </script>
</body>
</html>
